#include "State.h"
#include "BoxTweaker.h"
#include "Editor.h"
namespace FinalRendering
{
	namespace Editor
	{
		State::State()
		{
			MouseDragging = false;
		}

		void State::DrawState_3DView(View3D * view)
		{

		}

		void State::DrawState_LeftView(ViewLeft* view)
		{

		}

		void State::DrawState_TopView(ViewTop * view)
		{

		}

		void State::DrawState_FrontView(ViewFront * view)
		{

		}

		void State::KeyDown(View * view, UIKeyEventArgs & e)
		{
			
		}

		void State::UpdateSelection()
		{
			Scene * Doc = WorldEditor->Scene.operator ->();
			WorldEditor->Selection.Clear();
			if (!WorldEditor->EditTexcoord)
			{
				for (int i=0; i<Doc->Entities.Count(); i++)
				{
					if (Doc->Entities[i]->Selected)
					{
						Selection s;
						s.Type = Selection::otEntity;
						s.ID = i;
						s.SecondaryID = -1;
						WorldEditor->Selection.CombineItem(s);
					}	
				}
			}
			for (int i=0; i<Doc->Brushes.Count(); i++)
			{
				if (!WorldEditor->EditTexcoord)
				{
					if (Doc->Brushes[i]->Selected)
					{
						Selection s;
						s.Type = Selection::otBrush;
						s.ID = i;
						s.SecondaryID = -1;
						WorldEditor->Selection.CombineItem(s);
					}	
				}
				else
				{
					for (int j=0; j<Doc->Brushes[i]->Polygons.Count(); j++)
					{
						if (Doc->Brushes[i]->Polygons[j].Selected)
						{
							Selection s;
							s.Type = Selection::otBrush;
							s.ID = i;
							s.SecondaryID = j;
							WorldEditor->Selection.CombineItem(s);
						}
					}
				}
			}
		}

		void State::SnapToGrid(float gsize, vec3 &point)
		{
			if (!WorldEditor->SnapToGrid)
				return;
			float *v = (float *)&point;
			for (int i=0; i<3; i++)
			{
				int sgnp = (v[i]>Epsilon?1:(v[i]<-Epsilon?-1:0));
				v[i] = sgnp * Round( fabs(v[i]) / gsize ) * gsize;
			}
		}


		void State::MouseDown(View * view, UIMouseEventArgs & e)
		{
			MouseDragging = true;
			ldPosX = e.X;
			ldPosY = e.Y;
			lPosX = ldPosX;
			lPosY = ldPosY;
		}


		void State::MouseUp(View * view, UIMouseEventArgs & e)
		{
			MouseDragging = false;
		}

		void State::UpdateMousePosition(int x, int y)
		{
			if (MouseDragging)
			{
				lPosX = x;
				lPosY = y;
			}
		}

		void State::MouseMove(View * view, UIMouseEventArgs & e)
		{

		}

	}
}